//
//  HZPeiziMoneFlowVC.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/11/29.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziMoneFlowVC.h"
#import "HZPeiziMoneyFlowView.h"
#import "MYHttp.h"

@interface HZPeiziMoneFlowVC ()
@property (nonatomic, strong)HZPeiziMoneyFlowView *pMoneyFlowView;
@property (nonatomic, strong)NSArray *pTempData;

@property (nonatomic, assign)NSInteger nTime;// 发生时间 0:全部 1:7天之内 2:15天之内
@property (nonatomic, assign)NSInteger nFlow;// 收支  0:全部 1: 收 2:支
@end

@implementation HZPeiziMoneFlowVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    self.title = @"资金流水";
    
    //关联逻辑管理对象
    self.logicManager = [HZBaseLogjcManager new];
    self.view.logicManager = self.logicManager;
    
    _pMoneyFlowView = [HZPeiziMoneyFlowView new];
    [self.view addSubview:_pMoneyFlowView];
    [_pMoneyFlowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self p_registerBlock];
    
    _nFlow = 0;
    _nTime = 1;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    NSString *pStart = @"";
    NSString *pEnd = @"";
    [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:7];
    
    [self p_requestMoneyFlowWithStart:pStart end:pEnd];
}
- (void)commonToolYMDTimeWithStart:(NSString **)start
                               end:(NSString **)end
                            endDay:(int)endDay
                          rangeDay:(int)rangeDay
{
    double dCurrentTime = [[NSDate date] timeIntervalSince1970];//当天日期
    double dEndTime = dCurrentTime - endDay*24*60*60;//结束时间
    double dStartTime = dEndTime - (rangeDay-1)*24*60*60;//开始时间
    *end = [self commonToolYMDTimeStrWithTimeInterval:dEndTime];
    *start = [self commonToolYMDTimeStrWithTimeInterval:dStartTime];
}

- (NSString *)commonToolYMDTimeStrWithTimeInterval:(int)timeInterval
{
    NSDate *time = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyyMMdd"];
    NSString *timeStrT = [formatter stringFromDate:time];
    return timeStrT;
}

- (void)filterFlowWithContent:(NSArray *)content
{
    NSString *flow = content[0];
    NSString *time = content[1];
    if ([flow isEqual:@"全部"] || [flow isEqual:@"收/支"]) {
        _nFlow = 0;
    }else if ([flow isEqual:@"收入"]) {
        _nFlow = 1;
    }else if ([flow isEqual:@"支出"]) {
        _nFlow = 2;
    }
    
    NSString *pStart = @"";
    NSString *pEnd = @"";
    if ([time isEqual:@"全部"] || [time isEqual:@"发生日期"]) {
        _nTime = 0;
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:0];
        pStart = @"20171102";
    }else if ([time isEqual:@"7天之内"]) {
        _nTime = 1;
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:7];
    }else if ([time isEqual:@"15天之内"]) {
        _nTime = 2;
        [self commonToolYMDTimeWithStart:&pStart end:&pEnd endDay:0 rangeDay:15];
    }
    [self p_requestMoneyFlowWithStart:pStart end:pEnd];
}
- (void)fileterData
{
    if (_nFlow == 0) {
        [_pMoneyFlowView updateAccountData:_pTempData];
        return;
    }
    NSMutableArray *temp = [NSMutableArray array];
    for (HZPeiziTradeFlowListModel *model in _pTempData) {
        if (_nFlow == 1) {
            if ([model.bstype isEqual:@"卖出"]) {
                [temp addObject:model];
            }
        }else if (_nFlow == 2) {
            if ([model.bstype isEqual:@"买入"]) {
                [temp addObject:model];
            }
        }
    }
    [_pMoneyFlowView updateAccountData:temp];
}

#pragma mark -
#pragma mark blockAction
- (void)p_registerBlock
{
    __weak typeof (self)weakSelf = self;
    [self.logicManager mapLogic:kLogicRouterPeiziFlowFind toBlock:^id(NSDictionary *params, id other) {
        [weakSelf filterFlowWithContent:other];
        return nil;
    }];
}

- (void)p_requestMoneyFlowWithStart:(NSString *)start
                                end:(NSString *)end
{
    [SVProgressHUD show];
    NSArray *array = @[@{@"funcid":@"17", @"session":[HZUserManager manager].peiziAccountSession, @"startdate":start, @"enddate":end}];
    [MYHttp post:kAPIPeiziHost params:array success:^(id response) {
        NSMutableArray *tempArray = [NSMutableArray array];
        if ([response isKindOfClass:[NSArray class]]) {
            NSArray *array = (NSArray *)response;
            for (NSDictionary *dict in array) {
                HZPeiziTradeFlowListModel *model = [HZPeiziTradeFlowListModel mj_objectWithKeyValues:dict];
                [tempArray addObject:model];
            }
        }
        _pTempData = tempArray;
        [self fileterData];
//        [_pMoneyFlowView updateAccountData:_pTempData];
        [SVProgressHUD dismiss];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.domain];
    }];
}

@end
